package com.qiantu.phone.ui.activity;

import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.lifecycle.LifecycleOwner;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemDragListener;
import com.chad.library.adapter.base.module.BaseDraggableModule;
import com.chad.library.adapter.base.module.DraggableModule;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.hjq.base.BaseDialog;
import com.hjq.http.listener.HttpCallback;
import com.hjq.http.listener.OnHttpListener;
import com.qiantu.api.db.HandleListener;
import com.qiantu.api.db.LLDeviceDBManager;
import com.qiantu.api.db.LLRoomDBManager;
import com.qiantu.api.entity.DeviceBean;
import com.qiantu.api.entity.RoomBean;
import com.qiantu.api.http.LLHttpManager;
import com.qiantu.api.http.model.HttpData;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.event.RefreshDeviceListEvent;
import com.qiantu.phone.event.RefreshFloorRoomEvent;
import com.qiantu.phone.ui.dialog.ConfirmCancelHintDialog;

import org.greenrobot.eventbus.EventBus;
import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;

public class RoomSrotActivity extends AppActivity {
    private int mPosition;
    private TextView tvFloorName;
    private RecyclerView sceneList;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_room_sort;
    }

    @Override
    protected void initView() {
        sceneList = findViewById(R.id.room_list);
        tvFloorName = findViewById(R.id.tv_floor_name);
        tvFloorName.setText(getIntent().getStringExtra("floorName"));
        String floorSerialNo = getIntent().getStringExtra("floorSerialNo");
        sceneList.setLayoutManager(new LinearLayoutManager(getContext()));
        mAdapter = new RoomSrotActivity.RoomSortAdapter(LLRoomDBManager.getInstance(getContext()).getRoomByFloorSerialNo(floorSerialNo));
        mAdapter.addChildClickViewIds(R.id.delete);
        mAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                int id = view.getId();
                if (id == R.id.delete) {
                    showConfirmCancelHintDialog(getString(R.string.delete_room_hint_2), "取消", "删除", new ConfirmCancelHintDialog.OnListener() {
                        @Override
                        public void onLeftClick(BaseDialog dialog) {
                            dialog.dismiss();
                        }

                        @Override
                        public void onRightClick(BaseDialog dialog) {
                            dialog.dismiss();
                            Map data = new HashMap<>();
                            RoomBean roomBean = mAdapter.getData().get(position);
                            //1房间绑定 2房屋绑定(移除房间,绑定到房屋)
                            data.put("roomSerialNo", roomBean.getRoomSerialNo());
                            LLHttpManager.deleteRoom((LifecycleOwner) getActivity(), data,
                                    new HttpCallback<HttpData<Void>>((OnHttpListener) getActivity()) {
                                        @Override
                                        public void onStart(Call call) {
                                            showDialog(null);
                                        }

                                        @Override
                                        public void onSucceed(HttpData<Void> result) {
                                            if (result.isRequestSucceed()) {
                                                LLRoomDBManager.getInstance(getActivity()).remove(roomBean);
                                                mAdapter.removeAt(position);
                                                List<DeviceBean> roomDevices = LLDeviceDBManager.getInstance(getContext()).getDeviceByRoom(roomBean.getRoomSerialNo());
                                                //删除的房屋下有设备就刷新
                                                if(roomDevices != null && roomDevices.size() > 0){
                                                    LLDeviceDBManager.getInstance(getContext()).updateCommonDeviceByDeleteRoomSerialNo(roomBean.getRoomSerialNo());
                                                    EventBus.getDefault().post(new RefreshDeviceListEvent());
                                                }
                                                EventBus.getDefault().post(new RefreshFloorRoomEvent());
                                                setResult(RESULT_OK);
                                            }
                                        }

                                        @Override
                                        public void onFail(Exception e) {
                                            super.onFail(e);
                                        }
                                    });
                        }
                    });

                }
            }
        });
        mAdapter.getDraggableModule().setOnItemDragListener(new OnItemDragListener() {
            @Override
            public void onItemDragStart(RecyclerView.ViewHolder viewHolder, int i) {
                mPosition = i;
                viewHolder.itemView.setBackgroundResource(R.drawable.setting_item_selector_pressed);
            }

            @Override
            public void onItemDragMoving(RecyclerView.ViewHolder viewHolder, int i, RecyclerView.ViewHolder viewHolder1, int i1) {

            }

            @Override
            public void onItemDragEnd(RecyclerView.ViewHolder viewHolder, int i) {
//                if (mPosition != i) {
//                    getTitleBar().getRightView().setEnabled(true);
//                }
                if (mPosition != i) {
                    doSort();
                }
                viewHolder.itemView.setBackgroundResource(R.drawable.setting_item_pressed_bg);
            }
        });
        mAdapter.getDraggableModule().setDragEnabled(true);
        mAdapter.getDraggableModule().getItemTouchHelperCallback().setSwipeMoveFlags(ItemTouchHelper.START | ItemTouchHelper.END);
        sceneList.setAdapter(mAdapter);
    }

    @Override
    protected void initData() {
    }

    private RoomSrotActivity.RoomSortAdapter mAdapter;

    private class RoomSortAdapter extends BaseQuickAdapter<RoomBean, BaseViewHolder> implements DraggableModule {
        public RoomSortAdapter(List<RoomBean> data) {
            super(R.layout.scene_order_ly, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, RoomBean item) {
            helper.setText(R.id.name, item.getRoomName());

        }

        @NotNull
        @Override
        public BaseDraggableModule addDraggableModule(@NotNull BaseQuickAdapter<?, ?> baseQuickAdapter) {
            return new BaseDraggableModule(baseQuickAdapter);
        }
    }

    @Override
    public void onRightClick(View view) {
        doSort();
    }

    private void doSort() {
        LLHttpManager.roomSort(this, mAdapter.getData(), new HttpCallback<HttpData<Void>>(this) {
            @Override
            public void onSucceed(HttpData<Void> result) {
                super.onSucceed(result);
                LLRoomDBManager.getInstance(getActivity()).putInTx(mAdapter.getData(), new HandleListener() {
                    @Override
                    public void onCompleted() {
                        setResult(RESULT_OK);
                    }
                });
            }
        });
    }
}
